<?php


namespace app\common\library;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use think\facade\Log;

class WxmpMsg {

    const WXMP_MSG_CHARGE_TPL_ID = '57G2IilFFO3Rhc1D61qG7-5WLT2zu5xMmIkMVDd0V4c';
    const WXMP_MSG_TPL_ID_RIDE_START = 'xAZY2vsQO18OjxKggc7hm2db9mBqsnJyLnhchrioRbk';
    const WXMP_MSG_TPL_ID_RIDE_FINISH = 'S7xcuvMA7pgItgqixMmdZtw0qovIbqYmejmjQNtrjJw';

    function send($appid, $tmp_id, $page, $open_id, $form_id, $data) {
        $retry = 0;

        list($token, $expire) = (new AccessTokenModel())->get($appid);

        if (empty($token)) {
            Log::error("wxmpmsg send token = null");
            return;
        }

        while ($retry++ < 2) {
            $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$token}";
            $params = [
                'touser' => $open_id,
                'template_id' => $tmp_id,
                'page' => $page,
//                'form_id' => $form_id,
                'data' => $data,
            ];

            $ret = \common::curlpostjson($url, $params);
            if (!empty($ret) && isset($ret['errcode']) && $ret['errcode'] == 40001) {
                list($t, $ex) = (new AccessTokenModel())->refresh_wx_access_token($appid);
                $token = $t;
            } else {
                break;
            }
        }
    }

    function send_msg_charge($open_id, $prepay_id, $amount, $blance) {
        $appid = 'wxmpu';
        $tmp_id = self::WXMP_MSG_CHARGE_TPL_ID;
        $page = 'pages/tabs/home/home';

        $amount = round($amount / 100, 2) . "元";
        $blance = round($blance / 100, 2) . "元";
        $data = [
            'keyword1' => [
                'value' => $amount
            ], 'keyword2' => [
                'value' => $blance
            ],
        ];
        $form_id = $prepay_id;
        $this->send($appid, $tmp_id, $page, $open_id, $form_id, $data);
    }

    function send_msg_ride_start($form_id, $open_id) {
        $appid = 'wxmpu';
        $tmp_id = self::WXMP_MSG_TPL_ID_RIDE_START;
        $page = 'pages/tabs/home/home';

        $data = [
            'time1' => [
                'value' => date('Y-m-d H:i:s'),
            ],
            'thing2' => [
                'value' => '车辆开锁成功，请遵守交规'
            ],
        ];

        $this->send($appid, $tmp_id, $page, $open_id, $form_id, $data);
    }

    function send_msg_ride_finish($form_id, $open_id, $miles, $duration, $amount, $amount_detail) {
        $appid = 'wxmpu';
        $tmp_id = self::WXMP_MSG_TPL_ID_RIDE_FINISH;
        $page = 'pages/tabs/home/home';

        $data = [
            'date4' =>
                [
                    'value' => date('Y-m-d H:i:s'),
                ],
            'thing5' =>
                [
                    'value' => $duration
                ],
//            'keyword2' =>
//                [
//                'value' => $miles
//            ],
            'amount6' =>
                [
                    'value' => $amount
                ],
            'thing7' =>
                [
                    'value' => $amount_detail
                ],
        ];

        $this->send($appid, $tmp_id, $page, $open_id, $form_id, $data);
    }

}
